{% extends 'member-add.html' %}

{% block head %}
<style type="text/css">
    .display_none{display: none;}
</style>


{% endblock %}

{% block bodyweigh %}

{% endblock %}

{% block content %}

<!-- 右侧主体开始 -->
    <div class="page-content">
      <div class="content">
        <!-- 右侧内容框架，更改从这里开始 -->
        <form class="layui-form" >
            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red">*</span>游戏名称
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="name1" 
                    autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
            </div>
            
            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red">*</span>游戏下载地址
                </label>
                <div class="layui-input-inline">
                    <input type="text" name="name" autocomplete="off" class="layui-input" id="name3" value="" />
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
            </div>

            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red">*</span>游戏贴吧地址
                </label>
                <div class="layui-input-inline">
                    <input type="text" name="name" autocomplete="off" class="layui-input" id="name4" value="" />
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
            </div>
            
            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red">*</span>游戏icon
                </label>
                <div class="layui-input-inline">
                    <input type="file" class="addimage" accept="image/*"/><input type="hidden" class="addimgurl" value=""/>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
            </div>

            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red">*</span>游戏banner
                </label>
                <div class="layui-input-inline">
                    <input type="file" class="addbanner" accept="image/*"/><input type="hidden" class="addbannerurl" value=""/>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red">*</span>游戏优先级(可选)
                </label>
                <div class="layui-input-inline">
                    <div id="number" style="color:blue;size:"200px"><select id="num"></select></div>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
            </div>
            
            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">
                    <span class="x-red"></span>
                </label>
                <div class="layui-input-inline">
                    <button type="button"  style="background-color: #009688;border: none;color: #fff; width:80px;height:38px" class="addition">添加</button>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    
                </div>
            </div>
        <!-- 右侧内容框架，更改从这里结束 -->
      </div>
    </div>
    <!-- 右侧主体结束 -->

{% endblock %}

{% block script %}
    <script>


    function strDateTime(str) {
        var reg = /^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/;
        var r = str.match(reg);
        if(r==null) {
            return false;
        }else {
            return str;
        }
    }

    for (var i=1;i<100;i++)
    {
        $("#num").append('<option value ="'+i+'">'+i+'</option>');
    }

    $(".addimage").change(function(){
        var imageurl = $(this).next();
        var file = this.files[0];
        if (window.FileReader) {
                var reader = new FileReader();
                reader.readAsDataURL(file);
                //监听文件读取结束后事件
              reader.onloadend = function (e) {
                imageurl.val(e.target.result);    //e.target.result就是最后的路径地址
                };
           }
    });

    $(".addbanner").change(function(){
        var imageurl = $(this).next();
        var file = this.files[0];
        if (window.FileReader) {
                var reader = new FileReader();
                reader.readAsDataURL(file);
                //监听文件读取结束后事件
              reader.onloadend = function (e) {
                imageurl.val(e.target.result);    //e.target.result就是最后的路径地址
                };
           }
    });

   

    $(".addition").on("click",function(){

        var title = $("#name1").val();     
        var load_url = $("#name3").val();
        var tieba_url = $("#name4").val();
        var image = $(".addimgurl").val();  
        var addbannerurl = $(".addbannerurl").val();            
        var priority = $("#num option:selected").html();   

        if ( title == "" ) {
                alert("游戏名称的值不能为空！");
                return false;
            }
        
        if ( load_url == "" ) {
            alert("游戏下载地址的值不能为空！");
            return false;
        }
      
        if ( load_url == "" ) {
            alert("游戏贴吧地址的值不能为空！");
            return false;
        }
 
        if (image == ""){
            alert('请添加游戏icon');
            return false;
        } 
        if (addbannerurl == ""){
            alert('请添加游戏banner');
            return false;
        } 

        params = {
            "title":title,        
            "load_url":load_url,
            "tieba_url":tieba_url,
            "icon":image,
            "banner":addbannerurl,
            "priority":priority,
        };
    
        $.ajax({
            url:"/app/game/add",
            type:"POST",
            data:JSON.stringify(params),
            contentType:'application/json',
            success:function(res){
                if(res.status == 0){
                    alert("信息添加成功！");
                    location.href="/app/show/add_game";
                }else {
                    alert(res.msg);
                }
            }
        })
    });   


    </script>   
{% endblock %}